<?php
/**
 * MongoDig
 *
 * @copyright  Copyright (c) 2010 lanyanguang (http://www.lanyg.com)
 * @license    GNU General Public License 2.0
 * @version    $Id$
 */

//MongoDB连接
function connect()
{
        global $config;
        $connstr = '';
        if(!empty($config['db_host'])){
        	$connstr = 'mongodb://' . $config['db_host'];
        	if(!empty($config['db_port'])){
        		$connstr .= ':' . $config['db_port'];
        		if(!empty($config['db_username']) && !empty($config['db_password'])){
        			$connstr = 'mongodb://' . $config['db_username'] . ':' . $config['db_password'] . '@' . $config['db_host'] . ':' . $config['db_port'];
        		}
        	}else{
	        	if(!empty($config['db_username']) && !empty($config['db_password'])){
	        		$connstr = 'mongodb://' . $config['db_username'] . ':' . $config['db_password'] . '@' . $config['db_host'];
	        	}
        	}
        }
        return new Mongo($connstr);
}

//判读用户是否登录
function is_login()
{
	if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
		return $_SESSION['user_id'];
	}else{
		return FALSE;
	}
}

//分类列表
function cat_list()
{
	global $db;
	global $config;
	$collection = $db->cat;
	$cats = $collection->find();
	foreach($cats as $cat){
		echo '<a href="'.$config['site_url'].'/cat.php?cat_id=' .$cat['_id']. '">'.$cat['name'].'</a><br>';	
	}
}

//分页函数
function pagenav($total,$page,$perpage,$url,$posts=0) 
{
	$page_arr = array();
	$arr_count = 0;

	if($posts) 
	{
		$symb='&';
	}
	else
	{
		$symb='?';
	}
	$total_pages = ceil($total/$perpage);
	$llimit = 1;
	$rlimit = $total_pages;
	$window = 5;
	$html = '';
	if ($page<1 || !$page) 
	{
		$page=1;
	}
	
	if(($page - floor($window/2)) <= 0)
	{
		$llimit = 1;
		if($window > $total_pages)
		{
			$rlimit = $total_pages;
		}
		else
		{
			$rlimit = $window;
		}
	}
	else
	{
		if(($page + floor($window/2)) > $total_pages) 
		{
			if ($total_pages - $window < 0)
			{
				$llimit = 1;
			}
			else
			{
				$llimit = $total_pages - $window + 1;
			}
			$rlimit = $total_pages;
		}
		else
		{
			$llimit = $page - floor($window/2);
			$rlimit = $page + floor($window/2);
		}
	}
	if ($page>1)
	{
		$page_arr[$arr_count]['title'] = 'Prev';
		$page_arr[$arr_count]['link'] = $url.$symb.'page='.($page-1);
		$page_arr[$arr_count]['current'] = 0;
		
		$arr_count++;
	}

	for ($x=$llimit;$x <= $rlimit;$x++) 
	{
		if ($x <> $page) 
		{
			$page_arr[$arr_count]['title'] = $x;
			$page_arr[$arr_count]['link'] = $url.$symb.'page='.($x);
			$page_arr[$arr_count]['current'] = 0;
		} 
		else 
		{
			$page_arr[$arr_count]['title'] = $x;
			$page_arr[$arr_count]['link'] = $url.$symb.'page='.($x);
			$page_arr[$arr_count]['current'] = 1;
		}
		
		$arr_count++;
	}
	
	if($page < $total_pages)
	{
		$page_arr[$arr_count]['title'] = 'Next';
		$page_arr[$arr_count]['link'] = $url.$symb.'page='.($page+1);
		$page_arr[$arr_count]['current'] = 0;
		
		$arr_count++;
	}
	
	return $page_arr;
}

//获得分类
function get_cat()
{
    if(!isset($_GET['cat_id'])){
		return 0;
	}else{
		$cat_id = $_GET['cat_id'];
	}
	global $db;
	$collection = $db->cat;
	$cats = $collection->findOne(array('_id'=>new MongoId($cat_id)));
	if(!$cats){
		return 0;
	}
	return $cats;
}

//获得页面标题
function page_title($file)
{
	if($file=='cat'){
		if(!$str = get_cat()){
			return 'View All';
		}else{
			return $str['name'];
		}
	}
}

//获得IP
function get_ip(){
	if ($_SERVER['REMOTE_ADDR']) {
	$cip = $_SERVER['REMOTE_ADDR'];
	} elseif (getenv("REMOTE_ADDR")) {
	$cip = getenv("REMOTE_ADDR");
	} elseif (getenv("HTTP_CLIENT_IP")) {
	$cip = getenv("HTTP_CLIENT_IP");
	} else {
	$cip = "unknown";
	}
	return $cip;
}

//格式化时间
function time_taken($time)
{
	if($time > 86400)
	{
		$days = floor($time/86400);
		$hours = floor(($time-($days*86400))/3600);
		
		if($days > 1)
		{
			$took = $days . ' days';
		}
		else
		{
			$took = $days . ' day';
		}
	}
	elseif($time > 3600)
	{
		$hours = floor(($time/60)/60);
		$mins = floor(($time-($hours*3600))/60);
		
		if($hours > 1)
		{
			$took = $hours.' hours';
		}
		else
		{
			$took = $hours.' hour';
		}
	}
	elseif($time > 60)
	{
		$mins = floor($time/60);
	
		$took = $mins . ' minutes';
	}
	else
	{
		$took = $time . ' seconds';
	}
	
	return $took;
}